<template>
  <div class="login-container">
    <img class="index-bg" src="../../assets/bg5.jpg">
    <img class="logo-word" src="../../assets/logoWord.png" alt="#">

    <div id="Login">
      <div class="L-decoration">
        
      </div>
      
      <el-form class="login-form" autoComplete="on" :model="loginForm"  ref="loginForm" label-position="left">
        <!-- :rules="loginRules" -->
        <h3 class="title">考勤系统</h3>
        <el-form-item prop="username">
          <span class="svg-container svg-container_login">
            <svg-icon icon-class="user" />
          </span>
          <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="username" />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password"></svg-icon>
          </span>
          <el-input name="password" :type="pwdType" @keyup.enter.native="login" v-model="loginForm.password" autoComplete="on"
            placeholder="password"></el-input>
            <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%;" :loading="loading" @click.native.enter="login"> 
            <!-- @click.native.prevent="handleLogin" -->
            登录
          </el-button>
        </el-form-item>
        <!-- <div class="tips">
          <span style="margin-right:20px;">username: admin</span>
          <span> password: admin</span>
        </div> -->
      </el-form>
    </div>

    
  </div>
</template>

<script>
import { isvalidUsername } from "@/utils/validate";
import {router,dynamicRouter} from '../../router/index'
import qs from 'qs';

export default {
  name: "login",
  data() {
    // const validateUsername = (rule, value, callback) => {
    //   if (!isvalidUsername(value)) {
    //     callback(new Error("请输入正确的用户名"));
    //   } else {
    //     callback();
    //   }
    // };
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error("密码不能小于5位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: ""
      },
      // loginRules: {
      //   username: [
      //     { required: true, trigger: "blur", validator: validateUsername }
      //   ],
      //   password: [{ required: true, trigger: "blur", validator: validatePass }]
      // },
      loading: false,
      pwdType: "password",
      username: "",
      password: "",
      captcha: "",
      checked: ""
    };
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    login() {
      this.loading = true;
      this.$axios
        .post(
          "http://localhost:8088/login",
          qs.stringify({
            loginName: this.loginForm.username,
            password: this.loginForm.password
          }),
          {
            headers: {
              "Content-Type": "application/x-www-form-urlencoded"
            }
          }
        )
        .then(res => {
          console.log(res.data);
          this.loading = false;
          if(res.data.code == "200"){
            console.log(res.data.data.roleId)
            this.$store.commit('SET_ROLES', res.data.data.roleId)
            this.$store.commit('user_id', res.data.data.userId)
            window.localStorage.setItem('roleId',res.data.data.roleId);
            window.localStorage.setItem('userName',res.data.data.realName);
            window.localStorage.setItem('userid',res.data.data.userId);
      
             this.$router.push({ path: "/dashboard" }); //跳转到用户主界面
          }else{
            this.$notify.error({
              title: '失败',
              message: '登录失败，请检查用户名密码是否正确',
              type: 'error'
            });
          }
          
        
        })
        .catch(function(err) {
          console.log(err);
        });
    },
    handleLogin() {}
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: black;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;

  .login-form {
    position: absolute;
    top: 10%;
    right: 0;
    width: 294px;
    padding: 0 10px 10px 10px;

    margin: 0 auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title {
    font-size: 22px;
    font-weight: 400;
    color: #45b4e2;
    line-height: 30px;
    margin: 0px auto;
    text-align: center;
    font-weight: bold;
    margin-bottom: 20px;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
<style lang="scss">
//登录背景
.index-bg {
  width: 100%;
  height: 100%;
  position: absolute;
}
.logo-word {
  width: 500px;
  position: absolute;
  top: 4%;
  right: 2%;
}
#Login {
  position: absolute;
  top: 20%;
  right: 8%;
  width: 300px;
  height: 345px;
  background: white;
  border: 3px solid #45b4e2;
  border-radius: 10px;
  .L-decoration {
    position: absolute;
    top: 0;
    left: 0;
    border-left: 20px solid #45b4e2;
    border-right: 20px solid transparent;
    border-top: 15px solid #45b4e2;
    border-bottom: 15px solid transparent;
    box-shadow: 3px 3px 3px #ddd;
  }
}
</style>

